<template>
  <el-container>
    <el-aside class="w-[200px] h-[100vh] bg-[#f0f0f0] overflow-auto">
      <div class="flex gap-4 flex-col p-4">
        <div v-for="tag in filterTags" class="cursor-pointer flex justify-between gap-4"
          :class="tag.warn ? 'text-red-600' : ''" :key="tag.prop" :disable-transitions="false" @click="popEdit(tag)">
          <span class="truncate" :class="curTag == tag ? 'text-yellow-600' : ''">{{ tag.prop }} {{ tag.label }}</span>
          <span class="text-red-700 text-nowrap" @click.stop="deleteTag(tag)">删除</span>
        </div>
      </div>
    </el-aside>
    <el-main class="h-[100vh] overflow-auto">
      <el-drawer v-model="drawer" title="属性设置" direction="rtl">
        <h4>属性</h4>
        <div class="p-4">
          <el-form-item label="名称">
            <el-input v-model="curTag.label" />
          </el-form-item>
          <el-form-item label="是否禁止编辑">
            <el-switch v-model="curTag.disabled" />
          </el-form-item>

          <el-form-item label="是否必填">
            <el-switch v-model="curTag.required" />
          </el-form-item>


          <el-form-item label="搜素标签宽度：">
            <el-input v-model="curTag.searchLabelWidth" />
          </el-form-item>

          <el-form-item label="快速搜素">
            <el-switch v-model="curTag.search" />
          </el-form-item>

          <el-form-item label="字典">
            <el-input v-model="curTag.dicName" />
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="curTag.type" placeholder="Select">
              <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </div>
      </el-drawer>

      <div class="common-layout">
        <el-tabs v-model="activeName" class="demo-tabs">
          <el-tab-pane label="list" name="list">
            <div class="p-4">
              <el-input type="textarea" :rows="10" placeholder="请输入JSON内容" v-model="jsonInput" class="mb-4" />
              <el-button type="primary" @click="onInit" class="w-full mb-4">
                确定
              </el-button>

              <h4>{{ dsName }}</h4>

              <div class="flex gap-4 items-center mt-4">
                <el-input v-model="width"></el-input>
                <el-button type="primary">确定</el-button>
                <el-input v-model="curName"></el-input>
                <el-button type="primary" @click="changeName">修改名称</el-button>

                <template v-if="curTag">
                  <div class="text-nowrap">禁止编辑：</div>
                  <el-switch v-model="curTag.disabled" />
                  <div class="text-nowrap">是否必填：</div>
                  <el-switch v-model="curTag.required" />
                  <div class="text-nowrap">搜素标签宽度：</div>
                  <el-input v-model="curTag.searchLabelWidth" />


                  <div class="text-nowrap">字典：</div>
                  <el-input v-model="curTag.dicName" @change="showLog"></el-input>
                  <!-- <el-button @click="changeDicName">确定</el-button> -->
                </template>

              </div>


              <div class="mt-8 p-4 shadow-lg">

                <div class="flex gap-4 items-center p-4 bg-[#f0f0f0]">
                  <div class="text-lg text-nowrap">搜索</div>
                  <div class="text-nowrap">筛选：</div><el-input v-model="keyword"></el-input>
                </div>

                <div class="flex gap-4 items-center mt-4">
                  <el-button type="danger" @click="filterList = []">清空</el-button>
                </div>

                <div class="flex gap-4 items-center mt-4 flex-wrap">
                  <el-tag v-for="tag in filterList" :type="tag == curTag ? 'warning' : 'info'" @click="curTag = tag"
                    class="cursor-pointer" :key="tag.prop" closable :disable-transitions="false"
                    @close="handleCloseFilter(tag)">
                    {{ tag.label }}
                  </el-tag>
                </div>

                <div class="my-3 h-1 bg-[#999]"></div>

                <div class="flex gap-4 items-center mt-4 flex-wrap">
                  <el-tag v-for="tag in filterTags" type="success" class="cursor-pointer" @click="filterList.push(tag)"
                    :key="tag.prop" :disable-transitions="false">
                    {{ tag.label }}
                  </el-tag>
                </div>
              </div>

              <div class="mt-8 p-4 shadow-lg">
                <div class="flex gap-4 items-center p-4 bg-[#f0f0f0]">
                  <div class="text-lg text-nowrap">表格</div>
                  <div class="text-nowrap">筛选：</div><el-input v-model="keyword"></el-input>
                </div>

                <div class="flex gap-4 items-center mt-4">
                  <el-button type="danger" @click="trList = []">清空</el-button>
                </div>

                <div class="flex gap-4 items-center mt-4 flex-wrap">
                  <el-tag v-for="tag in trList" :type="tag == curTag ? 'warning' : 'info'" @click="curTag = tag"
                    class="cursor-pointer" :key="tag.prop" closable :disable-transitions="false"
                    @close="handleCloseTr(tag)">
                    {{ tag.label }}
                  </el-tag>
                </div>

                <div class="my-3 h-1 bg-[#999]"></div>

                <div class="flex gap-4 items-center mt-4 flex-wrap">
                  <el-tag v-for="tag in filterTags" type="success" class="cursor-pointer" @click="trList.push(tag)"
                    :key="tag.prop" :disable-transitions="false">
                    {{ tag.label }}
                  </el-tag>
                </div>
              </div>

              <div class="mt-8 p-4 shadow-lg" v-for="(item, i) in group" :key="i">
                <div class="flex gap-4 items-center p-4 bg-[#f0f0f0]">
                  <div class="text-lg text-nowrap">{{ item.prop }} ({{ item.label || '未设置' }})</div>
                  <div class="text-nowrap">筛选：</div><el-input v-model="keyword"></el-input>
                </div>

                <div class="flex gap-4 items-center mt-4">
                  <el-button @click="onRow(item, 2)">2列</el-button>
                  <el-button @click="onRow(item, 3)">3列</el-button>
                  <el-button @click="onRow(item, 4)">4列</el-button>
                  <el-button type="danger" @click="item.column = []">清空</el-button>
                  <div class="text-nowrap">添加属性：</div>
                  <el-input v-model="newObj.prop" placeholder="属性名称"></el-input>
                  <el-input v-model="newObj.label" placeholder="标签"></el-input>
                  <!-- <el-input v-model="newObj.type" placeholder="input"></el-input> -->

                  <el-select v-model="newObj.type" placeholder="Select">
                    <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>

                  <el-button @click="onAdd">确定</el-button>
                </div>
                <div class="flex gap-4 items-center mt-4 flex-wrap">
                  <el-tag v-for="tag in item.column" :type="tag == curTag ? 'warning' : 'info'" @click="curTag = tag"
                    class="cursor-pointer" :key="tag.prop" closable :disable-transitions="false"
                    @close="handleCloseForm(item.column, tag)">
                    {{ tag.label }}
                  </el-tag>
                </div>

                <div class="my-3 h-1 bg-[#999]"></div>

                <div class="flex gap-4 items-center mt-4 flex-wrap">
                  <el-tag v-for="tag in filterTags" type="success" class="cursor-pointer" @click="item.column.push(tag)"
                    :key="tag.prop" :disable-transitions="false">
                    {{ tag.label }}
                  </el-tag>
                </div>
              </div>

              <el-button class="w-full my-8" type="danger" @click="onCreate">生成代码</el-button>


              <el-input type="textarea" :rows="10" placeholder="替换后的结果" v-model="result" readonly class="w-full mb-4" />
            </div>
          </el-tab-pane>
          <el-tab-pane label="item" name="item">item</el-tab-pane>
        </el-tabs>
      </div>

    </el-main>
  </el-container>

</template>

<script setup>
import { computed, reactive, ref } from 'vue';
import { checkDuplicateProp } from '@/comm/tool'
const width = ref(120)
const curTag = ref(null)
const curName = ref("")
const filterList = ref([])
const formList = ref([])
const group = ref([])
const keyword = ref("")
const activeName = ref("list")
const drawer = ref(false)

const newObj = ref({
  prop: '',
  label: '',
  type: ''
})

const onAdd = () => {
  const obj = {
    "type": newObj.value.type || "input",
    "span": 12,
    "order": 99,
    "addDisplay": true,
    "editDisplay": true,
    "viewDisplay": true,
    "prop": newObj.value.prop,
    "label": newObj.value.label
  }
  console.log("obj", obj)
  tabList.value.push(obj)
}

const jsonInput = ref(localStorage.getItem("str") || '');
const tabList = ref([])
const trList = ref([])
const result = ref("")
const dsName = ref("")
const typeOptions = reactive([
  {
    value: 'number',
    label: 'number',
  },
  {
    value: 'input',
    label: 'input',
  },
  {
    value: 'select',
    label: 'select',
  }
]);
const changeName = () => {
  if (curTag) {
    curTag.value.label = curName.value
  }
}

const showLog = () => {
  console.log("group", group.value)
  console.log("formList", formList.value)
  console.log("filterList", filterList.value)
  console.log("curTag", curTag.value)
}

const popEdit = (val) => {
  curTag.value = val
  drawer.value = true

}

const filterTags = computed(() => {
  if (keyword.value) {
    return tabList.value.filter(item => {
      return item.label.includes(keyword.value)
    })
  }
  return tabList.value;
})

const onRow = (item, n) => {
  item.span = 24 / n
}

const handleCloseFilter = (tag) => {
  filterList.value.splice(filterList.value.indexOf(tag), 1)
}


const handleCloseTr = (tag) => {
  trList.value.splice(trList.value.indexOf(tag), 1)
}

const deleteTag = (tag) => {
  tabList.value.splice(tabList.value.indexOf(tag), 1)
}


const handleCloseForm = (list, tag) => {
  list.splice(list.indexOf(tag), 1)
}

function onInit() {
  try {
    const json = JSON.parse(jsonInput.value);
    console.log(json)
    localStorage.setItem("str", jsonInput.value)
    checkDuplicateProp(json.column)
    dsName.value = json.dsName
    tabList.value = json.column;
    group.value = json.group
    trList.value = clone(json.column);
    filterList.value = json.column.filter(i => i.search)

    // result.value = modifiedLines.join('\n');
  } catch (error) {
    console.error('处理错误:', error);
    result.value = '处理错误，请检查输入内容。';
  }
}

const onCreate = () => {
  const json = JSON.parse(jsonInput.value);
  trList.value.forEach(item => {
    const res = filterList.value.find(i => i.label == item.label)
    if (res) {
      item.search = true
    }
  })
  json.column = clone(trList.value);
  json.group = clone(group.value);
  addDicName(json.column)
  json.group.forEach(item => {
    addDicName(item.column)
    if (item.span) {
      item.column.forEach(i => {
        i.span = item.span
      })
      delete item.span
    }
  })
  result.value = JSON.stringify(json, null, 2)
  console.log(json)
}

const clone = (obj) => {
  return JSON.parse(JSON.stringify(obj));
}
const addDicName = (list) => {
  list.forEach(i => {
    if (i.dicName) {
      i.type = "select";
      i.dicUrl = "/system/dict/dictionary?code=" + i.dicName;
      i.props = {
        label: "dictValue",
        value: "dictKey"
      }
      delete i.dicName
    }
  })
}
</script>
